<template>
  <div class="postlist">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  -->
    <el-table
      ref="singleTable"
      :data="postlist"
      highlight-current-row
      style="width: 100%"
      border
      stripe
    >
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column
        property="user.nickname"
        label="作者"
        width="220"
      ></el-table-column>
      <el-table-column property="title" label="标题"></el-table-column>
      <el-table-column label="类型" width="150">
        <template slot-scope="scope">{{
          scope.row.type != 2 ? "文章" : "视频"
        }}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--  -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="data.pageIndex"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="data.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { getPostlist } from "@/api/artical";
export default {
  data() {
    return {
      postlist: [],
      data: {
        pageIndex: 1,
        pageSize: 2,
      },
      currentRow: null,
      total: 0,
    };
  },

  methods: {
    // 编辑
    handleEdit(data) {
      // this.$refs.singleTable.setCurrentRow(row);
      this.$router.push({ path: `postpublish/${data.id}` });
      console.log(data);
    },
    // 删除
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    // 页码
    handleSizeChange(val) {
      console.log(val);
      this.data.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.data.pageIndex = val;
      this.init();
    },
    // 渲染
    async init() {
      // console.log(this.data, 2);
      const res = await getPostlist(this.data);
      // console.log(res);
      this.postlist = res.data.data;

      this.total =
        this.total > res.data.data.length ? this.total : res.data.data.length;
    },
  },
  async mounted() {
    const res = await getPostlist({ pageSize: 100000 });
    this.total = res.data.data.length;
    this.init();
  },
};
</script>

<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 20px;
}
</style>
